package holder;

import com.example.googlemarketdemo.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;

import utils.UIUtils;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewParent;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import base.BaseHolder;
import bean.AppInfoBean;

/**
 * 详情的描述部分
 * 
 * @author Anson-pc
 * 
 */
public class AppDetailDesHolder extends BaseHolder<AppInfoBean> implements
		OnClickListener {

	/*-----------  获取控件 ----------*/
	@ViewInject(R.id.app_detail_des_tv_author)
	TextView mTvAuthor;
	@ViewInject(R.id.app_detail_des_tv_des)
	TextView mTvDes;
	@ViewInject(R.id.app_detail_des_iv_arrow)
	ImageView mIvArrow;
	/*--------- end ----------*/

	// 定义一个视图展开或者隐藏的动画
	private boolean isOpen = true;
	private int mTvDesmeasuredHeight;

	private AppInfoBean mAppInfoBean;

	@Override
	protected View initHolderView() {
		View view = View.inflate(UIUtils.getContext(),
				R.layout.item_detail_des, null);
		//设置点击事件
		view.setOnClickListener(this);
		ViewUtils.inject(this, view);
		
		return view;
	}

	@Override
	protected void refreshHolderView(AppInfoBean data) {
		// 保存数据

		mAppInfoBean = data;

		// 绑定数据
		mTvAuthor.setText(data.author);

		mTvDes.setText(data.des);
		// 监听布局完成
		mTvDes.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						mTvDesmeasuredHeight = mTvDes.getMeasuredHeight();
						mTvDes.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);

						// 默认的逻辑
						toggleAnimation(false);
					}
				});
	}

	@Override
	public void onClick(View v) {
		//点击执行动画
		toggleAnimation(true);
	}

	public void toggleAnimation(boolean isAnimation) {
		if (isOpen) {// 折叠
			// 折叠：mTvDes的高度从应有的高度 --》7行的高度
			// 测量
			/*
			 * mTvDes.measure(0, 0); int measureHeight =
			 * mTvDes.getMeasuredHeight();
			 */

			int start = mTvDesmeasuredHeight;
			int end = getShortHeight(7, mAppInfoBean.des);// 获取文本的高度
			
			if (isAnimation) {
				doAnimation(start, end);
			} else {
				mTvDes.setHeight(end);
			}

		} else {
			// 打开：mTvDes的高度从7行的高度 --》应有的高度
			int start = getShortHeight(7, mAppInfoBean.des);
			int end = mTvDesmeasuredHeight;
			
			
			if (isAnimation) {
				doAnimation(start, end);
			} else {
				mTvDes.setHeight(end);
			}
		}
		isOpen = !isOpen;
	}

	/**
	 * 运行动画的方法
	 * 
	 * @param start
	 * @param end
	 */
	public void doAnimation(int start, int end) {
		ObjectAnimator animator = ObjectAnimator.ofInt(mTvDes, "height", start,
				end);
		animator.start();
		//监听动画的执行过程
		animator.addListener(new AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animator) {//动画开始
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animator animator) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animator animator) {//动画结束
				//找到Sc'rollView ,进行滚动
				ViewParent parent = mTvDes.getParent();
				while (true) {
					parent = parent.getParent();
					if (parent == null) {
						break;
					}
					
					if (parent instanceof ScrollView) {
						//滚动 滚动到ScrollView的底部
						((ScrollView) parent).fullScroll(View.FOCUS_DOWN);
						break;
					}
				}
				
			}
			
			@Override
			public void onAnimationCancel(Animator animator) {
				// TODO Auto-generated method stub
				
			}
		});

		// 箭头的动画
		if (isOpen) {
			ObjectAnimator.ofFloat(mIvArrow, "rotation", 180, 0).start();
		} else {
			ObjectAnimator.ofFloat(mIvArrow, "rotation", 0, 180).start();
		}
	}

	/**
	 * 
	 * @param i
	 * @param 获取7行文本的高度
	 * @return
	 */
	private int getShortHeight(int line, String des) {
		TextView tempTextView = new TextView(UIUtils.getContext());
		tempTextView.setText(des);
		tempTextView.setLines(line);

		tempTextView.measure(0, 0);
		int measureHeight = tempTextView.getMeasuredHeight();

		return measureHeight;
	}
}
